package com.platform.core.web.controller;

import com.platform.core.service.SysMsgRecordBiz;
import com.platform.dto.SysMsgRecordVo;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.comm.web.results.JsonResult;
import com.platform.comm.web.results.PageListJsonResult;
import com.platform.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;
import java.util.Map;


/**
 * Description: 消息记录-管理接口
 *
 * @author Coder
 */
@RestController
@RequestMapping(name = "消息记录管理", path = "/management/lcodeMsgRecord")
public class SysMsgRecordController extends BaseController {

    private final SysMsgRecordBiz sysMsgRecordBiz;

    @Autowired
    public SysMsgRecordController(SysMsgRecordBiz sysMsgRecordBiz) {
        this.sysMsgRecordBiz = sysMsgRecordBiz;
    }

    /**
     * 分页查询
     */
    @PostMapping(name = "分页查询", path = "/search")
    public PageListJsonResult<Map<String, Object>> search(@RequestBody Map<String, String> map) {
        Page<Map<String, Object>> entities = sysMsgRecordBiz.search(map,getPageable(map));
        return new PageListJsonResult<>(entities);
    }

    /**
     * 记录详情
     */
    @PostMapping(name = "记录详情", path = "/detail")
    public DataJsonResult<SysMsgRecordVo> detail(@RequestBody Map<String, String> map) {
        SysMsgRecordVo entities = sysMsgRecordBiz.detail(map);
        return DataJsonResult.succeed(entities);
    }

    /**
     * 一键已读
     */
    @PostMapping(name = "一键已读", path = "/modifyReadStatus")
    public JsonResult modifyReadStatus() {
        sysMsgRecordBiz.modifyReadStatus();
        return JsonResult.success();
    }


    /**
     * 删除
     */
    @PostMapping(name = "删除", path = "/deleteById")
    public JsonResult deleteById(@RequestBody Map<String, String> map) {
        sysMsgRecordBiz.deleteById(map);
        return JsonResult.success();
    }

    /**
     * 分页查询记录
     */
    @PostMapping(name = "分页查询记录", path = "/searchRecord")
    public PageListJsonResult<SysMsgRecordVo> searchRecord(@RequestBody Map<String, String> map) {
        Page<SysMsgRecordVo> entities = sysMsgRecordBiz.searchRecord(map, getPageable(map));
        return new PageListJsonResult<>(entities);
    }




    /**
     * 改变已读状态
     */
    @GetMapping(name = "改变已读状态", path = "/editReadStatus")
    public void editReadStatus(HttpServletResponse response, String id) {
        System.out.println("id:" + id);
        sysMsgRecordBiz.editReadStatus(id);
        try {
            //设置输出文件格式
            response.setContentType("image/png");
            //将Base64格式的图片解码成字节
            byte[] bytes = Base64.getDecoder().decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR42mP4//8/AAX+Av4zEpUUAAAAAElFTkSuQmCC");
            InputStream inStream = new ByteArrayInputStream(bytes);
            OutputStream outputStream = null;
            outputStream = new BufferedOutputStream(response.getOutputStream());

            //创建存放文件内容的数组
            byte[] buff = new byte[1024];
            //所读取的内容使用n来接收
            int n;
            //当没有读取完时,继续读取,循环
            while ((n = inStream.read(buff)) != -1) {
                //将字节数组的数据全部写入到输出流中
                outputStream.write(buff, 0, n);
            }
            //强制将缓存区的数据进行输出
            outputStream.flush();
            //关流
            outputStream.close();
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
